#coding=utf-8
# import MySQLdb
import xlwt

# conn= MySQLdb.connect(
#         host='localhost',
#         port = 3306,
#         user='root',
#         passwd='lytz888',
#         db ='mysite',
#         )
try :
    import cx_Oracle
    user = 'ats'
    passwd = 'ats'
    # orcl = 'localhost:1521/hdrocl.webserver'
    orcl = '192.168.15.45/lytzats'
    conn = cx_Oracle.connect(user, passwd, orcl)
except Exception :
    # import MySQLdb
    # conn = sqlite3.connect('db.sqlite3')

    # conn= MySQLdb.connect(
    #         host='localhost',
    #         port = 3306,
    #         user='root',
    #         passwd='lytz888',
    #         db ='mysite',
    #         )

    import sqlite3
    conn = sqlite3.connect('db.test')
cursor = conn.cursor()
def sqltoExcel(sql):
    print sql
    cursor.execute(sql)
    res = cursor.fetchall()
    #不要字段名的情况
    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet("sqltoExcel")

    row_num = 0
    for row in res:
        #print row
        col_num = 0
        for filed in row:
            if row_num == 0:
                ws.col(col_num).width = 0x1d00
            ws.write(row_num, col_num, filed,set_style('Arial',220,True))
            col_num += 1
        row_num += 1
    wb.save("test.xls")
    return wb
def set_style(name,height,bold=False):
  style = xlwt.XFStyle() # 初始化样式

  font = xlwt.Font() # 为样式创建字体
  font.name = name # 'Times New Roman'
  # font.bold = bold
  font.color_index = 4
  font.height = height

  borders= xlwt.Borders()
  borders.left= 5
  borders.right= 5
  borders.top= 5
  borders.bottom= 5

  style.font = font
  # style.alignment = xlwt.Alignment()
  style.borders = borders

  return style


if __name__ == "__main__":
    sql = "select * from disk_user"
    sqltoExcel(sql)
